package com.zllkj.mm.module.home.fragments;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zllkj.mm.R;
import com.zllkj.mm.api.network.RetrofitHelper;
import com.zllkj.mm.api.network.RxHelper;
import com.zllkj.mm.api.network.subscriber.BaseRxSubscriber;
import com.zllkj.mm.base.BaseTitleFragment;
import com.zllkj.mm.common.L;
import com.zllkj.mm.model.entity.LoginVo;
import com.zllkj.mm.module.UIHelper;
import com.zllkj.mm.module.general.fragments.LoginFragment2;
import com.orhanobut.hawk.Hawk;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by leon_ok.
 */

public class SetPwdFragment extends BaseTitleFragment {

    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.etRePwd)
    EditText etRePwd;
    @BindView(R.id.confirm)
    TextView confirm;
//    @BindView(R.id.tvNumber)
//    TextView tvNumber;

    public static SetPwdFragment newInstance() {
        return new SetPwdFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_setpwd;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setCommonTitle("设置密码");
//        tvNumber.setText(LoginFragment2.loginS.phone.substring(0, 3) + " **** "
//                + LoginFragment2.loginS.phone.substring(LoginFragment2.loginS.phone.length() - 4));
    }

    @OnClick(R.id.confirm)
    public void onViewClicked() {
        if (TextUtils.isEmpty(etPwd.getText().toString()) || TextUtils.isEmpty(etRePwd.getText().toString())) {
            L.showToast("请输入密码");
            return;
        }
        if (!etPwd.getText().toString().equals(etRePwd.getText().toString())) {
            L.showToast("两次密码不一致");
            return;
        }
        addSubscribe(
                RetrofitHelper.getInstance().getApi()
                        .setPassword(new HashMap() {{
                            put("password", etPwd.getText().toString());
                      }},LoginFragment2.loginS.token)
                        .compose(RxHelper.<LoginVo>defaultHandleResult())
                        .subscribe(new BaseRxSubscriber<LoginVo>() {
                            @Override
                            protected void onSucc(LoginVo o) {
                                Hawk.put(L.iM_userId, LoginFragment2.loginS.userId);
                                Hawk.put(L.token, LoginFragment2.loginS.token);
                                Hawk.put(L.imToken, LoginFragment2.loginS.imToken);
                                //设置密码后强制完善个人资料
                               // addFragment(EditInfoFragment.newInstance());
                                UIHelper.showPage(EditInfoFragment.newInstance());
                            }
                        })
                  );
    }

}


